<template>
  <div class="app-container ticket">
    <div class="ticket-header">
      <span>工单管理</span>
    </div>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="name"
        label="工单编号">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="问题描述">
        <template slot-scope="scope">
          <span class="ticket-des">{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="问题分类"
        width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.kind }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="提交时间"
        width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time ? new Date(Date.parse(new Date(scope.row.create_time))).Format("yyyy-MM-dd hh:mm:ss") : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="提交人"
        width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="状态"
        width="100">
        <template slot-scope="scope">
          <span :class="{'status-undo': scope.row.status === 0}">{{ scope.row.status === 0 ? '未处理' : scope.row.status === 2 ? '用户已删除' : '已处理' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <a class="table-action" @click="handleView(scope.row)">查看</a>
          <a :class="{'table-action-disable': scope.row.status === 2}" class="table-action" @click="handleTicket(scope.row)">处理</a>
        </template>
      </el-table-column>
    </el-table>
    <div class="basePagenate">
      <el-pagination
        :page-size="pageSize"
        :current-page="pageNumber"
        :page-count="total"
        layout="prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      :visible.sync="viewTicketDialog"
      class="ticket-view-dialog"
      title=""
      width="60%"
      center>
      <div class="ticket-view-box">
        <h1 class="ticket-title">工单编号：{{ viewTicket.id }}</h1>
        <div class="ticket-info-box box-grey">
          <div class="ticket-info-row">
            <div class="row-item">
              <span>工单状态：{{ viewTicket.status === 0 ? '未处理' : '已处理' }}</span>
            </div>
            <div class="row-item">
              <span>问题分类：{{ viewTicket.kind }}</span>
            </div>
          </div>
          <div class="ticket-info-row">
            <div class="row-item">
              <span>提交人邮箱：{{ viewTicket.email ? viewTicket.email : '-' }}</span>
            </div>
            <div class="row-item">
              <span>提交人手机：{{ viewTicket.tel ? viewTicket.tel : '-' }}</span>
            </div>
          </div>
          <div class="ticket-info-row">
            <span>提交时间：{{ new Date(viewTicket.create_time).Format("yyyy-MM-dd hh:mm:ss") }}</span>
          </div>
          <div class="ticket-info-row">
            <div class="row-item">
              <span>处理人：{{ viewTicket.handler ? viewTicket.handler : '-' }}</span>
            </div>
            <div class="row-item">
              <span>处理时间：{{ viewTicket.handle_time ? new Date(viewTicket.handle_time).Format("yyyy-MM-dd hh:mm:ss") : '-' }}</span>
            </div>
          </div>
        </div>
        <h1 class="ticket-title">问题描述：</h1>
        <div class="ticket-info-box">
          <div class="ticket-info-row">
            <p>{{ viewTicket.description }}</p>
          </div>
        </div>
        <div class="seperator-line" />
        <h1 class="ticket-title">处理结果：</h1>
        <div class="ticket-info-box">
          <div class="ticket-info-row">
            <p>{{ viewTicket.handle_result ? viewTicket.handle_result : '-' }}</p>
          </div>
        </div>
        <div class="seperator-line" />
        <h1 class="ticket-title">附件：</h1>
        <div class="ticket-info-box">
          <div class="ticket-info-row">
            <a @click="_viewTicketAttach">{{ viewTicket.attach }}</a>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="handleTicketDialog"
      class="ticket-view-dialog"
      title=""
      width="60%"
      center>
      <div class="ticket-view-box">
        <h1 class="ticket-title">工单编号：{{ curHandleTicket.id }}</h1>
        <h1 class="ticket-title">问题描述：</h1>
        <div class="ticket-info-box">
          <div class="ticket-info-row">
            <p>{{ curHandleTicket.description }}</p>
          </div>
        </div>
        <div class="seperator-line" />
        <h1 class="ticket-title">处理结果：</h1>
        <div class="ticket-info-box">
          <div class="ticket-info-row">
            <el-form ref="handTicketForm" :model="handTicketForm" :rules="rules" class="handle-form">
              <el-form-item label="" prop="handleResult">
                <el-input v-model="handTicketForm.handleResult" :rows="5" type="textarea" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitHandle">提交</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTicketList, submitTicket } from '@/api/ticket'
import { mapGetters } from 'vuex'
import request from '@/utils/request'

export default {
  data() {
    return {
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      list: null,
      listLoading: true,
      viewTicketDialog: false,
      viewTicket: {},
      viewTicketDownloadUrl: '',
      handleTicketDialog: false,
      curHandleTicket: {},
      handTicketForm: {
        handleResult: ''
      },
      rules: {
        handleResult: [
          { required: true, message: '请填写处理结果', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'userName'
    ])
  },
  created() {
    this._getTicketList()
  },
  methods: {
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.pageNumber = 1
      this._getTicketList()
    },
    handleCurrentChange(val) {
      this.pageNumber = val
      this._getTicketList()
    },
    // 查看工单
    handleView(row) {
      this.viewTicket = row
      this.viewTicketDownloadUrl = `/apis/api/v1/orders/download?id=${row.id}&uuid=${row.uuid}&file_name=${row.attach}`
      this.viewTicketDialog = true
    },
    // 处理工单
    handleTicket(row) {
      if (row.status === 2) {
        return false
      }
      this.curHandleTicket = row
      this.handleTicketDialog = true
    },
    // 提交处理
    submitHandle() {
      this.$refs.handTicketForm.validate(valid => {
        if (valid) {
          const data = {
            id: this.curHandleTicket.id,
            handle_result: this.handTicketForm.handleResult,
            handler: this.userName
          }
          submitTicket(data).then(res => {
            if (res.message.code === 200) {
              this.$message({ message: '处理结果已提交', showClose: true, type: 'success' })
              this.handleTicketDialog = false
              this.handTicketForm.handleResult = ''
              this._getTicketList()
            } else {
              this.$message({ message: res.message.message, showClose: true, type: 'error' })
            }
          })
        }
      })
    },
    // 查询工单列表
    _getTicketList() {
      const params = {
        page_index: this.pageNumber,
        per_page_items: this.pageSize
      }
      getTicketList(params).then(res => {
        if (res.message.code === 0) {
          this.list = res.data.items
          this.total = res.data.total_page
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    // 下载工单附件
    _viewTicketAttach() {
      const attachUrl = `/apis/api/v1/orders/download?id=${this.viewTicket.id}&uuid=${this.viewTicket.uuid}&file_name=${this.viewTicket.attach}`
      request.get(attachUrl, {
        responseType: 'arraybuffer'
      }).then(res => {
        const fileContent = res
        const blob = new Blob([fileContent], { type: 'application/octet-stream' })
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(blob, this.viewTicket.attach)
        } else {
          // 创建新的URL表示指定的File对象或者Blob对象
          const URL = window.URL || window.webkitURL
          const aTag = document.createElement('a')
          aTag.style = 'display: none'
          aTag.download = this.viewTicket.attach
          aTag.href = URL.createObjectURL(blob)
          document.body.appendChild(aTag)
          aTag.click()
          document.body.removeChild(aTag)
          URL.revokeObjectURL(blob)
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.ticket {
  &-header {
    padding: 20px 10px;
    background: #ffffff;
    font-size: 14px;
    color: #303133;
    font-weight: bold;
    margin-bottom: 20px;
    .header-title {
      font-size: 14px;
      font-weight: 700;
    }
    .header-title-left {
      float: right;
      height: 40px;
      line-height: 40px;
    }
  }
  .ticket-des {
    color: #136AD9;
    letter-spacing: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    word-break: break-all;
    word-wrap: break-word;
  }
  label {
    font-weight: normal
  }
  .table-action {
    color: #4285f4;
  }
  .table-action-disable {
    color: #ccc;
    cursor: not-allowed;
  }
  .table-link {
    color: #4285f4;
    &:hover {
      cursor: pointer;
    }
  }
  .status-undo {
    color: #f68460;
  }
  .status-box {
    border-radius: 5px;
    padding: 0 5px;
    color: #000000;
  }
  .status-success {
    background: #63D4AD;
  }
  .status-run {
    background: #85D8F7;
  }
  .status-wait {
    background: #f39c12;
  }
  .status-stop {
    background: #E0E3E8;
  }
  .status-failure {
    background: #F87D8D
  }
  .ticket-view-dialog {
    .el-dialog__body {
      padding: 0;
    }
    .ticket-view-box {
      color: #5c5b5b;
      font-size: 14px;
      padding-bottom: 40px;
      .ticket-title {
        padding: 20px 40px 0;
        font-size: 14px;
        color: #5c5b5b;
      }
      .ticket-info-box {
        padding: 0 40px;
        .ticket-info-row {
          padding: 10px 0;
          display: flex;
          .row-item {
            width: 50%;
          }
          p {
            line-height: 30px;
            word-break: break-all;
            margin: 0;
          }
          .handle-form {
            width: 100%;
          }
        }
      }
      .seperator-line {
        border-bottom: 1px solid #ccc;
        margin: 0 40px;
      }
      .box-grey {
        background: #eee;
        margin-top: 15px;
      }
    }
  }
}
</style>
